function [rmse, rmseE, YhatE] = calc_rmse (vals, ratings, YcvTest, yhat)
% Parameters:
%   vals: ret val from lin_liblinear
%   ratings: [1 2 4 5] for real sets X and Xtest,
%            [2 1 5 4] for CV sets XcvTrain and XcvTest.
%   YcvTest: labels for test data
%   Yhat: predicted values
%
% Returns RMSE for direct predictions and RMSE for expected values, and expected
%   value of yhat.
%

    n = numel (YcvTest);

    % Calc RMSE for direct predictions
    rmse = sqrt (sum ((YcvTest - yhat) .^ 2) / n);

    % Calc expected value
    p = exp(vals);
    p = bsxfun(@times, p, 1./sum(p,2));
    YhatE = sum(bsxfun(@times, p, ratings),2);

    % Calc RMSE for expected values
    rmseE = sqrt (sum ((YcvTest - YhatE) .^ 2) / n);

    disp (fprintf ('RMSE %g, RMSE for expected values %g\n', rmse, rmseE));

end

